package wetogether.travelinfo.control;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import wetogether.travelinfo.service.UpdateTravelInfoService;
import wetogether.travelinfo.vo.TravelCommentVO;
import wetogether.travelinfo.vo.TravelFrVO;
import wetogether.travelinfo.vo.TravelInfoVO;
import wetogether.travelinfo.vo.TravelVacancyVO;
import wetogether.travelinfo.vo.TravelZoneVO;

public class UpdateTravelInfoController extends MultiActionController{
	private UpdateTravelInfoService updateTravelInfoService;
	public void setUpdateTravelInfoService(UpdateTravelInfoService updateTravelInfoService){
		this.updateTravelInfoService=updateTravelInfoService;
		System.out.println("UpdateTravelInfoService injection....");
	}
	//////////////////////////////////////////////
	// 1. Output 정보 값 가져오기
	/////////////////////////////////////////////
	/**
	 * 존재하는 지역 리스트 findZoneList
	 */
	public ModelAndView findZoneList(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		String change=request.getParameter("change");
		mv.addObject("locationId",request.getParameter("locationId"));
		mv.setViewName("place"+change+"_update");
		List<TravelZoneVO> zoneList=updateTravelInfoService.findTravelZoneList();
		mv.addObject("zoneList",zoneList);
		System.out.println("zoneList:"+zoneList);
		return mv;
	}
	/**
	 * 해당 지역에 존재하는 여행지 리스트 findPlaceList
	 */
	public ModelAndView findPlaceList(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("travelplace_list");
		int locationid=Integer.parseInt(request.getParameter("locationid"));
		String change=request.getParameter("change");
		mv.addObject("change",change);
		try{
			if(change.equals("zone")){ //지역정보인지, 아닌지 구분
				TravelZoneVO zoneVO=updateTravelInfoService.getTravelZone(locationid);
				if(zoneVO!=null){
					mv.addObject("zoneVO",zoneVO);
				}else{
					mv.addObject("Emessage",locationid+" 지역의 정보가 존재하지 않습니다!");
				}
			}else{
				List<String> infoList=updateTravelInfoService.findTravelPlaceList(locationid);
				if(infoList.size()!=0){
					mv.addObject("infoList",infoList);
				}else{
					mv.addObject("Emessage",locationid+" 지역의 여행지가 존재하지 않습니다!");
				}
			}
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 1. 해당 여행지 정보를 가져온다 getTravelInfo
	 */
	public ModelAndView getTravelInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infooutput_result");
		String place=request.getParameter("place");
		String change=request.getParameter("change");
		System.out.println("change:"+change);
		mv.addObject("flag",change);
		try{
			if(change.equals("info")){
				TravelInfoVO tVO=updateTravelInfoService.getTravelInfo(place);
				if(tVO==null){
					mv.addObject("Emessage",place+" 여행지가 존재하지 않습니다!");
				}else{
					mv.addObject("vo",tVO);
				}
			}else if(change.equals("image")){
				List<String> imgList=updateTravelInfoService.getPlaceImageList(place);
				if(imgList.size()==0){
					mv.addObject("Emessage",place+" 여행지의 이미지가 존재하지 않습니다!");
				}else{
					mv.addObject("list",imgList);
				}
			}else if(change.equals("vacancy")){
				List<TravelVacancyVO> vList=updateTravelInfoService.getPlaceVacancyList(place);
				if(vList.size()==0){
					mv.addObject("Emessage",place+" 여행지의 숙박 정보가 존재하지 않습니다!");
				}else{
					mv.addObject("list",vList);
				}
			}else if(change.equals("fr")){
				List<TravelFrVO> fList=updateTravelInfoService.getPlaceFrList(place);
				if(fList.size()==0){
					mv.addObject("Emessage",place+" 여행지의 맛집 정보가 존재하지 않습니다!");
				}else{
					mv.addObject("list",fList);
				}
			}else if(change.equals("notice")){
				TravelInfoVO tVO=updateTravelInfoService.getTravelInfo(place);
				if(tVO.getNotice()==null){
					mv.addObject("Emessage",place+" 여행지의 공지가 존재하지 않습니다!");
				}else{
					mv.addObject("str",tVO.getNotice());
				}
			}else if(change.equals("comment")){
				List<TravelCommentVO> cList=updateTravelInfoService.getPlaceCommentList(place);
				System.out.println("cList:"+cList);
				if(cList.size()==0){
					mv.addObject("Emessage",place+" 여행지의 댓글이 존재하지 않습니다!");
				}else{
					mv.addObject("list",cList);
				}
			}
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 2. 회원 본인의 댓글을 가져온다
	 */
	public ModelAndView getPlaceComment(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infooutput_result");
		mv.addObject("flag","commentValue");
		TravelCommentVO commentVO=new TravelCommentVO();
		commentVO.setPlace(request.getParameter("place"));
		commentVO.setMemberid(request.getParameter("memberid"));
		try{
			TravelCommentVO cVO=updateTravelInfoService.getPlaceComment(commentVO);
			if(cVO==null){
				mv.addObject("Emessage",commentVO.getPlace()+","+commentVO.getMemberid() +"의 댓글이 존재하지 않습니다!");
			}else{
				mv.addObject("comment",cVO);
			}
		}catch(SQLException se){
			mv.addObject("Emessage", se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	
	//////////////////////////////////////////////
	// 2. Update 새로운 값으로 수정하기
	/////////////////////////////////////////////
	/**
	 * 0. 해당 지역의 정보를 수정한다 updateTravelZone
	 */
	public ModelAndView updateTravelZone(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoupdate_result");
		TravelZoneVO zoneVO=new TravelZoneVO();
		bind(request,zoneVO);
		try{
		updateTravelInfoService.updateTravelZone(zoneVO);
		mv.addObject("update",
				zoneVO.getLocation()+" 지역정보 update OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 1. 해당 여행지 정보를 수정한다 updateTravelInfo
	 */
	public ModelAndView updateTravelInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoupdate_result");
		TravelInfoVO tVO=new TravelInfoVO();
		bind(request,tVO);
		try{
		updateTravelInfoService.updateTravelInfo(tVO);
		mv.addObject("update",
				tVO.getPlace()+"의 여행정보 update OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 2. 해당 여행지의 이미지 정보를 수정한다 updatePlaceImage
	 */
	public ModelAndView updatePlaceImage(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoupdate_result");
		Map imgMap=new HashMap();
		String place=request.getParameter("place");
		String gallery=request.getParameter("gallery");
		String data=request.getParameter("data");
		imgMap.put("place", place);
		imgMap.put("gallery", gallery);
		imgMap.put("data", data);
		try{
			updateTravelInfoService.updatePlaceImage(imgMap);
		mv.addObject("update",
				place+"의 "+gallery+" update OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 3. 해당 여행지의 숙박 정보를 수정한다 updatePlaceVacancy
	 */
	public ModelAndView updatePlaceVacancy(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoupdate_result");
		TravelVacancyVO vacancyVO=new TravelVacancyVO();
		bind(request,vacancyVO);
		try{
			updateTravelInfoService.updatePlaceVacancy(vacancyVO);
		mv.addObject("update",
				vacancyVO.getPlace()+"의 "+vacancyVO.getStay()+" update OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 4. 해당 여행지의 맛집 정보를 수정한다 updatePlaceFr
	 */
	public ModelAndView updatePlaceFr(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoupdate_result");
		TravelFrVO frVO=new TravelFrVO();
		bind(request,frVO);
		try{
			updateTravelInfoService.updatePlaceFr(frVO);
		mv.addObject("update",
				frVO.getPlace()+"의 "+frVO.getRestaurant()+" update OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 5. 해당 여행지의 공지사항을 수정한다 updatePlaceNotice
	 */
	public ModelAndView updatePlaceNotice(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoupdate_result");
		TravelInfoVO tVO=new TravelInfoVO();
		tVO.setPlace(request.getParameter("place"));
		tVO.setNotice(request.getParameter("notice"));
		try{
			updateTravelInfoService.updatePlaceNotice(tVO);
		mv.addObject("update",
				tVO.getPlace()+"의 공시사항 update OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 6. 해당 여행지 회원의 댓글을 수정한다 updatePlaceComment
	 */
	public ModelAndView updatePlaceComment(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoupdate_result");
		TravelCommentVO commentVO=new TravelCommentVO();
		commentVO.setPlace(request.getParameter("place"));
		commentVO.setMemberid(request.getParameter("memberid"));
		commentVO.setContent(request.getParameter("content"));
		try{
			updateTravelInfoService.updatePlaceComment(commentVO);
		mv.addObject("update",
				commentVO.getPlace()+"의 "+commentVO.getMemberid()+"님 댓글 update OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
}
